<?php
/**
* 文章表数据类
*/
error_reporting(E_ALL^E_NOTICE^E_WARNING);
class type {
    /**
    * 
    * @var array
    */
    public $table = 'type';
    public $tid = null;

    public $data = array();  // row
    public $list = array();  // list

    public $listpage= null;  // listpage  对象
    public $tree = null;     // tree 对象 
/***************** 以上都是 类的 变量 ************************/

    /**
     *  PHP5版本 构造函数  需要写在php4 构造函数  前 
     *
     * @access    private
     * @param     string  $tplfile  模板文件
     * @return    void
     */
    function __construct( $arr=null )
    {

    	

    	//在构造函数中读取 分类 列表 并且初始化 为 tree
        $this->tree = new tree();
        $typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
        $this->list = &$this->tree->init( $typelist )->arr;//初始化数据  

        foreach($this->list as $k => $v){
            $v['typeurl'] = $this->typeurl($v['tid']);
            $this->list[$k] = $v;
         }
        


    }
    /**
     *  兼容PHP4版本  构造函数与 类名相同
     *
     * @access    private
     * @param     string  $tplfile  模板文件
     * @return    void
     */
    function type( $arr=null ){
        $this->__construct( $arr );
    }



    /**
     * 初始化 
     * @param  [int] $id [当前文章id]
     * @return [array]      读取成功 返回当前记录数组
     * @return [false]      读取失败 返回假
     */
    function init($tid = null){
    	$tid = intval($tid);
        

        $rs = $this->getRow($tid);
        
        if($rs){
        	$this->tid = $tid;
        	$this->data= $rs;
        }
    	
        return $this->data;   
    }

    
    /**
     * 获取一行记录  返回数组 
     * @param  $id [description]
     * @return [type]     [description]
     */
    public function getRow($tid = null){
    	$tid = intval($tid);

        $rs = $this->tree->get($tid);
        if(!$rs) { return $rs ; }
        $rs['typeurl'] = $this->typeurl($rs['tid']);
        return $rs;
    }


    /**
     * 获取文章 url
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function arturl($id = null){
         if(!$id){$id = $this->id;}
         return arturl($id);  //arturl 写在 inc/function.php中
    }


    /**
     * 获取文章 url
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function typeurl($tid = null){
         if(!$tid){$tid = $this->data['tid'];}
         return typeurl($tid);//typeurl 写在 inc/function.php中
    }


    /**
     * 过滤 列表中 display=1
     * @param  [type] $rs [description]
     * @return [type]     [description]
     */
    public function filter_display( &$list ){
        foreach($list as $k => $v){
            if($v['display'] != 1 ){ unset($list[$k]) ;}

        }
    }

    //顶层栏目
    public function top(){
    	$arr =array();
    	foreach($this->list as $k =>  $v){
    		if($v['pid'] == 0 ){
    			$arr[] = $v ;
    		}
    	}
        $this->filter_display( $arr );
    	return $arr;

    }

    //上级栏目 如果没有上级栏目 返回顶级栏目
    public function parent( $tid = null , $flag = false ){
    	if(!$tid){$tid = $this->tid ;}

    	$rs =  $this->tree->getPP( $tid ) ;
        $this->filter_display( $rs );

    	if($rs){
    		return $rs;
    	}
    	if($flag){
    		return $rs;
    	}else{
    		return $this->top();
    	}

    }
    //子级栏目 如果没有子级栏目 返回同级栏目
    public function child($tid = null , $flag = false){
    	if(!$tid){$tid = $this->tid;}

    	$rs =  $this->tree->getC( $tid ) ;
        $this->filter_display( $rs );
    	if($rs){
    		return $rs;
    	}
    	if($flag){
    		return $rs;
    	}else{
    		return $this->self();
    	}

    }
    //同级栏目 
    public function self($tid = null){
    	if(!$tid){$tid = $this->tid;}
    	$rs =  $this->tree->getS( $tid ) ;
        $this->filter_display( $rs );
    	return $rs;

    }

    //当前位置
    public function place($tid = null){
    	if(!$tid){$tid = $this->tid;}
    	$rs =  $this->tree->getPlace( $tid ) ;
        
    	return $rs;

    }



    //分页列表
    public function dl( $pageSize = null ){

    	$pageSize =  intval($pageSize);
    	if(!$pageSize){ $pageSize = 10 ;}

    	$tid = $this->tid;
    	$ids = $this->tree->getIds( $tid );

    	$dl = new ListPage();
		$dl->pageSize = $pageSize; //每页显示多少数据
		$dl->SetParameter('tid',$tid ); 
		$dl -> SetSql("select a.*  , b.typename
                from `article` as a
                left join  `type` as b on a.tid = b.tid 
                where a.recycle=0 and a.display = 1 and a.tid in ({$ids})
                order by `sorttime` desc "); //执行sql语句 注意 sql不要有 结尾分号
		$list = $dl ->GetList();//获取列表$list
		$page = $dl ->GetPage();//获取分页html代码

		foreach( $list as $k => $v){
            $v['arturl'] =  $this->arturl($v['id']);
            $v['typeurl'] = $this->typeurl($v['tid']);
            $list[$k] = $v;
        }

		return array( 'list' => $list , 'page' => $page );

    }

 
    
    
}
